<template>
  <div class="_box">
    <div>
      <a-select
        style="width: 100%"
        placeholder="请选择要登录的账号"
        @change="handleChange"
      >
        <a-select-option v-for="(itme, index) in info.userInfo" :key="index">
          {{ itme.user }}
        </a-select-option>
      </a-select>
    </div>
    <div>
      <span class="edit" @click="edit">编辑</span>
      <span class="remove" @click="remove">删除</span>
    </div>
    <div>
      <a-button type="primary" block @click="login"> 确认登录 </a-button>
    </div>
  </div>
</template>
<script>
const child = require("child_process");
const { resolve } = require("path");
export default {
  data() {
    return {
      info: {
        userInfo: [],
      },
      loginUser: null,
    };
  },
  computed: {},
  mounted() {
    this.getUser();
  },
  methods: {
    remove() {
      let that = this;

      this.readFile((data) => {
        data.userInfo.forEach((item, index) => {
          if (item.user === that.loginUser.user) {
            data.userInfo.splice(index, 1);
          }
        });
        that.writeFile({ ...data });
        that.loginUser = { user: null, pswd: null };
        that.getUser();
      });
    },
    edit() {
      this.$emit("edit", this.loginUser);
    },
    getUser() {
      //左侧添加账号触发右侧刷新
      let that = this;
      this.readFile((data) => {
        that.$set(that.info, "userInfo", data.userInfo);
      });
    },
    handleChange(i) {
      this.loginUser = this.info.userInfo[i];
    },
    login() {
      let t = this.remote.shell.writeShortcutLink(
        //使用shell创建快捷方式
        resolve("./") + "\\steam.lnk",
        "create",
        {
          target: resolve(this.url),
          args: `-login ${this.loginUser.user} ${this.loginUser.pswd}`, //拼入账号密码
        }
      );
      if (t) {
        //如果成功则运行快捷方式
        child.exec(
          `start ${resolve("./") + "\\steam.lnk"}`,
          (err, stdout, stderr) => {
            console.log(err, stdout, stderr);
          }
        );
      }
    },
  },
};
</script>
<style lang="less" scoped>
._box > div {
  margin-bottom: 32px;
  height: 32px;
}
._box > div:nth-of-type(2) {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  > span {
    margin: 0 10px;
  }
}
.edit {
  cursor: pointer;
  color: #1296db;
}
.remove {
  cursor: pointer;
  color: #ff0202;
}
</style>
